package com.hejia.bulbul.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hejia.alauda.entity.SplashScreenImage;
import com.hejia.alauda.entity.SplashScreenSchedule;
import com.hejia.alauda.service.SplashScreenService;
import com.hejia.alauda.utils.HttpResponse;
import com.hejia.alauda.utils.Pager;

/**
 * 启动画面显示排期控制器
 * @author hyr
 *
 */
@Controller
@RequestMapping("splashscreenschedules")
public class SplashScreenScheduleController extends BaseController {
	
	private static final Log Logger = LogFactory.getLog(SplashScreenScheduleController.class);
	
	@Resource
	private SplashScreenService splashScreenService;
	
	@RequestMapping
	public String list(Boolean isShow, String description, Pager<SplashScreenSchedule> pager, ModelMap model) {
		
		if (isShow == null) {
			isShow = true;
		}
		
		pager = splashScreenService.findSplashScreenSchedules(isShow, description, pager);
		model.put("schedules", pager);
		model.put("isShow", isShow);
		model.put("description", description);
		
		return "splashccreenschedule/list";
	}
	
	@RequestMapping("add")
	public String add(ModelMap model) {
		
		List<SplashScreenImage> iOSImages = createIOSSplashScreenImages();
		List<SplashScreenImage> androidImages = createAndroidSplashScreenImages();
		SplashScreenSchedule schedule = new SplashScreenSchedule();
		schedule.setDevice(SplashScreenSchedule.Device.iOS);
		schedule.setPriority(1);
		schedule.setImages(iOSImages);
		
		model.put("schedule", schedule);
		model.put("iOSImages", iOSImages);
		model.put("androidImages", androidImages);
		
		return "splashccreenschedule/add";
	}
	
	@RequestMapping(value="add", method = RequestMethod.POST)
	public String add(SplashScreenSchedule schedule, ModelMap model) {
		
		try {
			this.splashScreenService.addSplashScreenSchedule(schedule);
			return "redirect:/splashscreenschedules";
		} catch (Exception e) {
			Logger.error("添加启动画面错误", e);
			model.put("error", "添加启动画面错误 " + (e.getMessage() == null ? "" : e.getMessage()));
			model.put("schedule", schedule);
			model.put("iOSImages", createIOSSplashScreenImages());
			model.put("androidImages", createAndroidSplashScreenImages());
			return "splashccreenschedule/add";
		}
	}
	
	@RequestMapping("{id}/edit")
	public String edit(@PathVariable Long id, ModelMap model) {
		
		SplashScreenSchedule schedule = this.splashScreenService.findSplashScreenScheduleById(id);
		model.put("schedule", schedule);
		
		return "splashccreenschedule/edit";
	}
	
	@RequestMapping(value = "{id}/edit", method = RequestMethod.POST)
	public String edit(SplashScreenSchedule schedule, ModelMap model) {
		
		try {
			this.splashScreenService.updateSplashScreenSchedule(schedule);
			return "redirect:/splashscreenschedules";
		} catch (Exception e) {
			model.put("error", "更新启动画面排期错误 " + e.getMessage());
			model.put("schedule", schedule);
			return "splashccreenschedule/edit";
		}
	}
	
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public HttpResponse delete(@PathVariable Long id) {
		HttpResponse response = new HttpResponse();
		try {
			this.splashScreenService.deleteSplashScreenScheduleById(id);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("删除启动画面排期成功");
		} catch (Exception e) {
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("删除启动画面排期失败");
		}
		return response;
	}

	private List<SplashScreenImage> createAndroidSplashScreenImages() {
		
		List<SplashScreenImage> images = new ArrayList<>();
		images.add(createSplashScreenImage(480, 800, 240));
		images.add(createSplashScreenImage(720, 1280, 320));
		images.add(createSplashScreenImage(1080, 1920, 480));
		
		return images;
	}


	private List<SplashScreenImage> createIOSSplashScreenImages() {
		
		List<SplashScreenImage> images = new ArrayList<>();
		images.add(createSplashScreenImage(640, 960, 0));
		images.add(createSplashScreenImage(640, 1136, 0));
		images.add(createSplashScreenImage(750, 1334, 0));
		images.add(createSplashScreenImage(1242, 2208, 0));
		
		return images;
	}
	
	/**
	 * 创建启动画面图片
	 * @param width		宽度
	 * @param height	高度
	 * @param dpi		dpi
	 * @return
	 */
	private SplashScreenImage createSplashScreenImage(Integer width, Integer height, Integer dpi) {
		
		SplashScreenImage image = new SplashScreenImage();
		image.setWidth(width);
		image.setHeight(height);
		image.setDpi(dpi);
		
		return image;
	}
}
